#include "dominion.c"
#include "assert.h"
#include "stdio.h"
#include "rngs.c"

void checkGainCard(struct gameState *state, int *cards);

int main(){
	int *cards = kingdomCards(7,8,9,10,11,12,13,14,15,16);
	struct gameState *state = newGame();
	
	initializeGame(4, cards, -1, state);
	
	for (int i = 0; i < 20; i++){
		checkGainCard( state, cards );
	}
	
	return 0;
}

void checkGainCard(struct gameState *state, int *cards){
	int card = cards[(int)(Random() * 10)] + 3; // Picks from 7 valid and 3 invalid cards
	int flag = (int)(Random() * 3); // Randomly send the card to deck, hand or discard
	int player = (int)(Random() * 3.99); // Picks a random player; 3.99 because a 4 would cause a segfault
	
	int supply = state->supplyCount[card]; // Save the previous values
	int deck = state->deckCount[player];
	int hand = state->handCount[player];
	int discard = state->discardCount[player];
	
	if( !gainCard(card, state, flag, player) ){
		if ( supply != state->supplyCount[card] + 1 ){ // supplyCount should've gone down on a successful gain
			printf("Gain happened, supply count mismatch! Previous supply: %d. Current supply: %d\n", supply, supplyCount(card, state));
			assert(0);
		}
		switch (flag){
		case 1:
			if ( deck != state->deckCount[player] - 1 ){
				printf("Gain happened, deck size mismatch! Previous size: %d. Current size: %d\n", deck, state->deckCount[player]);
				assert (0);
			}
			break;
		case 2:
			if ( hand != state->handCount[player] - 1 ){
				printf("Gain happened, hand size mismatch! Previous size: %d. Current size: %d\n", hand, state->handCount[player]);
				assert (0);
			}
			break;
		default:
			if ( discard != state->discardCount[player] - 1 ){
				printf("Gain happened, discard size mismatch! Previous size: %d. Current size: %d\n", discard, state->discardCount[player]);
				assert (0);
			}
			break;
		}
	} else {
		if ( supply != state->supplyCount[card] ){	// Shouldn't've changed on a failed gain
			printf("Gain failed, supply count mismatch! Previous supply: %d. Current supply: %d\n", supply, supplyCount(card, state));
			assert(0);
		}
		switch (flag) {
		case 1:
			if ( deck != state->deckCount[player] ){
				printf("Gain failed, deck size mismatch! Previous size: %d. Current size: %d\n", deck, state->deckCount[player]);
				assert (0);
			}
			break;
		case 2:
			if ( hand != state->handCount[player] ){
				printf("Gain failed, hand size mismatch! Previous size: %d. Current size: %d\n", hand, state->handCount[player]);
				assert (0);
			}
			break;
		default:
			if ( discard != state->discardCount[player] ){
				printf("Gain failed, discard size mismatch! Previous size: %d. Current size: %d\n", discard, state->discardCount[player]);
				assert (0);
			}
			break;
		}
	}
}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		